/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.simplelabentry.web.controller.lab;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Concept;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.LabService;
import org.openmrs.module.hospitalcore.model.Lab;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;


/**
 *
 */
public class LabValidator implements Validator {

	/**
     * @see org.springframework.validation.Validator#supports(java.lang.Class)
     */
    public boolean supports(Class clazz) {
    	return Lab.class.equals(clazz);
    }

	/**
     * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
     */
    public void validate(Object command, Errors error) {
    	Lab lab = (Lab) command;
    	
    	if( StringUtils.isBlank(lab.getName())){
    		error.reject("billing.name.required");
    	}
    	
    	LabService labService = (LabService)Context.getService(LabService.class);
    	List<Lab> labs = labService.getAllLab();
		Integer labId = lab.getLabId();
		if (labId == null) {
			if (labService.getLabByName(lab.getName())!= null) {
				error.reject("billing.name.existed");
			}
			if( lab.getInvestigationsToDisplay() != null )
			{
				for( Concept con : lab.getInvestigationsToDisplay()){
					if( labService.getLabByConcept(con, labs) != null ){
						error.reject("billing.investigation.existed");
						break;
					}
				}
			}
			if( lab.getConfidentialTestsToDisplay() != null )
			{
				for( Concept con : lab.getConfidentialTestsToDisplay()){
					if( labService.getLabByConcept(con, labs) != null ){
						error.reject("billing.confidentialTest.existed");
						break;
					}
				}
			}
		} else {
			Lab dbStore = labService.getLabById(labId);
			if (!dbStore.getName().equalsIgnoreCase(lab.getName())) {
				if (labService.getLabByName(lab.getName()) != null) {
					error.reject("billing.name.existed");
				}
			}
			if( lab.getInvestigationsToDisplay() == null )
			{
				error.reject("billing.investigation.required");
			}else {
				for( Concept con : lab.getInvestigationsToDisplay()){
					Lab foundLab = labService.getLabByConcept(con, labs);
					if( foundLab != null  &&  foundLab != dbStore ){
						error.reject("billing.investigation.existed");
						break;
					}
				}
			}
			if( lab.getConfidentialTestsToDisplay() != null ){
				for( Concept con : lab.getConfidentialTestsToDisplay()){
					Lab foundLab = labService.getLabByConcept(con, labs);
					if( foundLab != null  &&  foundLab != dbStore  ){
						error.reject("billing.confidentialTest.existed");
						break;
					}
				}
			}
		}
		
    }

}
